package com.changjiang.luban.utils.data;

import com.changjiang.luban.dao.domain.sys.SysPermission;
import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author changjiang
 * Date: 2018-03-24
 * Time: 16:03
 */
@Data
public class Tree implements Serializable{

    private static final long serialVersionUID = -1177070514117634268L;

    private String id;
    private String text;
    private Map<String,Object> state;
    private boolean checked = false;
    private Object attributes;
    private String icon;
    private List<Tree> children;
    private String iconCls;
    private String pid;

    public static List<Tree> objectToTree(List<SysPermission> l){
        List<Tree> lt = new ArrayList<Tree>();
        if ((l != null) && (l.size() > 0)) {
            for (SysPermission r : l) {
                if(r.getPermission()==null){
                    Tree tree = new Tree();
                    tree.setId(r.getId().toString());
                   /* tree.setState("closed");*/
                    tree.setIcon(r.getIcon());
                    tree.setText(r.getName());
                    tree.setIconCls(r.getIcon());
                    List<Tree> children = new ArrayList<>();
                    for (SysPermission x: l) {
                        if(x.getPermission()!=null&&x.getPermission()==r){
                            Tree childrenT = new Tree();
                            childrenT.setId(x.getId().toString());
                            childrenT.setPid(x.getPermission().getId().toString());
                            childrenT.setIcon(x.getIcon());
                            childrenT.setText(x.getName());
                            childrenT.setIconCls(x.getIcon());
                            Map<String, Object> attr = new HashMap<String, Object>(1);
                            attr.put("url", x.getUrl());
                            childrenT.setAttributes(attr);
                            children.add(childrenT);
                        }
                    }
                    tree.setPid(null);
                    tree.setChildren(children);
                    lt.add(tree);
                }
            }
        }
        return lt;
    }


    public static List<Tree> treeSign(List<Tree> lt,List<Tree> userT){
        List<Tree> resultList = new ArrayList<>();
        for (Tree tree:lt) {
            if(tree.getChildren()!=null){
                List<Tree> children = tree.getChildren();
                List<Tree> resulChildren = new ArrayList<>();
                for (Tree childrenTree:children) {
                    for (Tree user: userT) {
                        List<Tree> userChildren = user.getChildren();
                        for (Tree userChildrenTree:userChildren) {
                            if(childrenTree.getId().equals(userChildrenTree.getId())){
                                Map<String, Object> attr = new HashMap<>(1);
                                attr.put("selected", true);
                                childrenTree.setState(attr);
                            }
                        }
                    }
                    resulChildren.add(childrenTree);
                }
                tree.setChildren(resulChildren);
            }
            resultList.add(tree);
        }
        return resultList;
    }
}
